package com.hifar.test.api.controller;

import com.hifar.sys.BusiResponse;
import com.hifar.test.entrust.constant.EntrustConsts;
import com.hifar.test.entrust.pojo.HfEnvEntrust;
import com.hifar.test.entrust.pojo.VO.HfEnvEntrustDebugSaveVO;
import com.hifar.test.entrust.pojo.VO.HfEnvEntrustSaveVO;
import com.hifar.test.entrust.service.IHfEnvEntrustService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import java.util.Set;

@RestController
@RequestMapping("/api/busi/test/api/entrust-api")
@Api(tags = "数据接收端接口")
public class EntrustApiController {

    private final Validator validator = Validation.buildDefaultValidatorFactory().getValidator();

    @Autowired
    private IHfEnvEntrustService hfEnvEntrustService;

    @PostMapping(value = "/saveEntrust")
    @ApiOperation(value = "委托单功能:新增")
    public BusiResponse saveEntrust(@RequestBody HfEnvEntrustDebugSaveVO hfEnvEntrustSaveVO) {
        hfEnvEntrustSaveVO.setDataSource(EntrustConsts.TS_DATA_SOURCE);
        Set<ConstraintViolation<HfEnvEntrustDebugSaveVO>> validate = validator.validate(hfEnvEntrustSaveVO);
        if (!validate.isEmpty()) {
            StringBuilder errBuilder = new StringBuilder();
            for (ConstraintViolation<HfEnvEntrustDebugSaveVO> constraintViolation : validate) {
                String message = constraintViolation.getMessage();
                errBuilder.append(message).append(";");
            }
            return BusiResponse.error(400, errBuilder.toString());
        }
        HfEnvEntrust hfEnvEntrust = hfEnvEntrustService.saveDebugEntrust(hfEnvEntrustSaveVO);
        return BusiResponse.OK(hfEnvEntrust);
    }
}
